<template>
  <div class="wrap" id="children4">
    <el-dialog
      title="修改密码"
      :visible.sync="state"
      @close="close"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @open="open"
    >
      <el-form>
        <el-form-item label="输入原密码" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="输入新密码" :label-width="formLabelWidth">
          <el-input
            autocomplete="new-password"
            type="text"
            @focus="changeType"
            v-model="newPassword"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" :label-width="formLabelWidth">
          <el-input
            autocomplete="new-password"
            type="text"
            @focus="changeType"
            v-model="newPassword2"
          ></el-input>
        </el-form-item>
        <el-form-item label="输入手机验证码" :label-width="formLabelWidth">
          <div class="sendCode">
            <el-input autocomplete="off" v-model="checkCode"></el-input>
            <el-button type="primary" @click="getSendMS">{{ms}}</el-button>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="close(1)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { sendSms } from "../utils/interface";
export default {
  props: {
    passworldState: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formLabelWidth: "120px",
      oldPassword: "",
      newPassword: "",
      checkCode: "",
      newPassword2: "",
      ms: "获取验证码",
      bok: true
    };
  },
  computed: {
    state: {
      get() {
        return this.passworldState;
      },
      set() {}
    }
  },
  methods: {
    close(v) {
      if (v) {
        if (!this.checkCode) {
          this.$alert("请输入验证码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.oldPassword) {
          this.$alert("请输入旧密码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.newPassword) {
          this.$alert("请输入新密码", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (this.newPassword.length < 6) {
          this.$alert("密码不能少于6位", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (this.newPassword !== this.newPassword2) {
          this.$alert("两次新密码输入不一致", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        let data = {
          oldPassword: this.oldPassword,
          newPassword: this.newPassword,
          checkCode: this.checkCode
        };
        this.$emit("getpass", data);
      } else {
        this.$emit("getpass");
      }
    },
    changeType(e) {
      e.srcElement.type = "password";
    },
    getSendMS() {
      let tel = JSON.parse(localStorage.getItem("userInfo")).account;
      if (!this.bok) {
        return false;
      }
      this.bok = false;
      let n = 60;
      sendSms({ account: tel }).then(res => {
        let timer = setInterval(() => {
          this.ms = `${n}`;
          n--;
          if (n <= 0) {
            this.bok = true;
            this.ms = "重新获取验证码";
            n = 60;
            clearInterval(timer);
          }
        }, 1000);
      });
    },
    open() {
      (this.oldPassword = ""),
        (this.newPassword = ""),
        (this.checkCode = ""),
        (this.newPassword2 = ""),
        (this.ms = "获取验证码");
    }
  }
};
</script>
<style lang="scss" scoped>
.el-input {
  max-width: 330px;
}
.el-button {
  max-width: 112px;
}
.sendCode {
  display: flex;
  align-items: center;
  .el-input {
    width: auto;
    margin-right: 10px;
  }
}
</style>
<style lang="scss">
#children4 .el-dialog {
  width: 30% !important;
  min-width: 400px;
}
</style>